package com.example.demo.rabbitMQ.controller;

import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>Description: </p>
 *
 * @author Eric Lee
 * @version v1.0.0
 * @since 2021/8/3 10:15
 **/

@RestController
public class SendMessageController {

    //  指定dev服务器rabbitTemplate
    @Resource(name = "devRabbitTemplate")
    AmqpTemplate devAmqpTemplate;

    @Autowired
    @Qualifier("rabbitTemplate")
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    // 利用rabbitTransactionManager事务管理
    @Transactional(rollbackFor = Exception.class, transactionManager = "rabbitTransactionManager")
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "hello!测试";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageContext", messageData);
        map.put("sendTime", createTime);
        //将消息携带绑定键值：orange 发送到交换机directExchange
        // 队列directqueue1和路由orange绑定关系
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("directExchange", "orange", JSONUtil.toJsonStr(map), correlationId);
        return "ok";
    }

    @GetMapping("/sendTopicMessage")
    public String sendTopicMessage() {
        Gson gson = new Gson();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageContext = "Topic消息,手动确认消息!!!";
        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, String> messageMap = new HashMap<>();
        messageMap.put("messageId", messageId);
        messageMap.put("messageContext", messageContext);
        messageMap.put("sendTime", sendTime);
        rabbitTemplate.convertAndSend("topicExchange", "MAX.orange.bb", gson.toJson(messageMap), new CorrelationData(messageId));
        return "ok";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        Gson gson = new Gson();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageContext = "广播交换器(fan out扇形交换器)";
        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, String> messageMap = new HashMap<>();
        messageMap.put("messageId", messageId);
        messageMap.put("messageContext", messageContext);
        messageMap.put("sendTime", sendTime);
        //CorrelationData 发送成功回调
        rabbitTemplate.convertAndSend("fanoutExchange", null, gson.toJson(messageMap), new CorrelationData(messageId));
        return "ok";
    }

}
